package com.pxx.item.service.impl;

import com.pxx.common.utils.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxx.item.pojo.TbBrand;
import com.pxx.item.service.TbBrandService;
import com.pxx.item.mapper.TbBrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
* @author Yoo19
* @description 针对表【tb_brand(品牌表，一个品牌下有多个商品（spu），一对多关系)】的数据库操作Service实现
* @createDate 2025-06-23 19:39:44
*/
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand>
    implements TbBrandService{

    @Autowired
    private TbBrandMapper tbBrandMapper;

    @Override
    public PageResult<TbBrand> queryBrandPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        //首先构建查询语句的构造器
        QueryWrapper<TbBrand> queryWrapper = new QueryWrapper<>();
        //判断key是否为空 不为空则拼接查询语句
        if(!StringUtils.isBlank(key)){
            queryWrapper.like("name",key);
        }
        //分页
        Page<TbBrand> brandPage = new Page<>(page, rows);
        //判断排序是否为空m不为空则拼接语句
        if(!StringUtils.isBlank(sortBy)){//如果desc为true列表数据为倒叙反之为正序
            brandPage.addOrder(desc? OrderItem.desc(sortBy):OrderItem.asc(sortBy));
        }
        //执行查询语句
        Page<TbBrand> pageInfoData = tbBrandMapper.selectPage(brandPage,queryWrapper);
        if(pageInfoData.getTotal() == 0){
            return  new PageResult<TbBrand>();
        }

        //数据的封装和整合
        PageResult<TbBrand> result = new PageResult<>(pageInfoData.getTotal(), pageInfoData.getPages(), pageInfoData.getRecords());
        return result;
    }

    @Transactional
    @Override
    public void saveBrand(TbBrand tbBrand, List<Long> cids) {
        //插入品牌数据
        int inserted = tbBrandMapper.insert(tbBrand);
        if(inserted > 0){
            //成功后,需要跟中间表绑定，因为品牌和分类是多对多的关系
            //因为mybatis是存在持久层,再插入时，只要当前事物没有离开范围

            //新增品牌和分类中间表
            cids.forEach(cid->{
                tbBrandMapper.saveCategoryAndBrand(cid,tbBrand.getId());
            });
        }
    }

    @Override
    public void updateBrand(TbBrand tbBrand, List<Long> cids) {
        //先删除中间表数据
        tbBrandMapper.deleteCategoryAndBrand(tbBrand.getId());
        //修改品牌数据
        tbBrandMapper.updateById(tbBrand);
        //新增中间表
        cids.forEach(cid ->{
            tbBrandMapper.saveCategoryAndBrand(cid,tbBrand.getId());
        });
    }

    @Override
    public void deleteBrand(Long id) {
        //先查询是否有数据
        TbBrand tbBrand = tbBrandMapper.selectById(id);
        //同时删除中间表数据
        tbBrandMapper.deleteCategoryAndBrand(id);
        //最后删除品牌表
        tbBrandMapper.deleteById(tbBrand);
    }

    @Override
    public TbBrand queryNamesById(Long brandId) {

        return tbBrandMapper.selectById(brandId);
    }

    @Override
    public List<TbBrand> queryBrandByCid(Long cid) {

        return tbBrandMapper.queryBrandByCid(cid);
    }
}




